package ntd.util.frame.action.function.map.geo.json;

import cn.hutool.core.convert.Convert;
import cn.hutool.db.Entity;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.UtilCatch;
import ntd.util.config.common.bo.RequestInfo;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;

import java.util.List;

public class ActionGeoJson extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("GeoJson服务", "action-geo-json", new ActionGeoJson());
    }

    @Override
    public ActionOutBo handle() {

        ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource(actionInBo.getParamStr("_dsId")));

        return UtilCatch.tryCatch(() -> {
            List<Entity> entities = toolMysql.db.findBy("data_geojson", "adcode", actionInBo.getDataCode());
            if (entities.size() < 1) {
                throw new ComException("找不到对应数据");
            }
            if (entities.size() > 1) {
                throw new ComException("数据异常：该code对应多个geojson数据");
            }
            return new ActionOutBo(entities.get(0).getStr("geojson"));
        });
    }

}
